set +x
set -eu; set -o pipefail

DockerManagerHost={{.DockerManagerHost}}
DockerManagerUser={{.DockerManagerUser}}
DockerManagerPort={{.DockerManagerPort}}

BackendServiceName={{.BackendServiceName}}
CeleryWorkerServiceName={{.CeleryWorkerServiceName}}
CeleryBeatServiceName={{.CeleryBeatServiceName}}
DstDBPort={{.Port}}

SrcDBHost=10.110.0.13
SrcDBPort=15432
SrcDBPassword='xxxxxxxx'
# uat db info
# SrcDBHost=10.110.0.11
# SrcDBPort=25432
# 

DstDBHost=10.110.0.11
DstDBPassword=xxxxx

DBName=test_db_v1
DBUser=test_db_user

# stop services
ssh -p $DockerManagerPort $DockerManagerUser@$DockerManagerHost bash -c "\"\
   if sudo docker service ls | grep $BackendServiceName; then \
   sudo docker service scale $BackendServiceName=0; fi;\
   if sudo docker service ls | grep $CeleryBeatServiceName; then \
   sudo docker service scale $CeleryBeatServiceName=0; fi;\
   if sudo docker service ls | grep $CeleryWorkerServiceName; then \
   sudo docker service scale $CeleryWorkerServiceName=0; fi\""

pg_dump -x -O -c -C -h $SrcDBHost -p $SrcDBPort -U $DBUser \
   "password=$SrcDBPassword dbname=$DBName" | psql -h $DstDBHost \
   -p $DstDBPort -U $DBUser \
   "password=$DstDBPassword dbname=postgres"

echo "you have to start applications on restart job."
